package cn.edu.lzu.platformweb.controller;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.entity.Role;
import cn.edu.lzu.entity.Unit;
import cn.edu.lzu.service.CityService;
import cn.edu.lzu.service.RoleService;
import cn.edu.lzu.service.UnitService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

@Controller
public class UnitController {
    @Resource
    private UnitService unitService;
    @Resource
    private CityService cityService;
    @Resource
    private RoleService roleService;

    @ResponseBody
    @RequestMapping("/checkOperatorLogin")
    public ResponseResult checkOperatorLogin(String username, String password){
        System.out.println("username:"+username+"\t password:"+password);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMsg("登录成功");
        responseResult.setCode(1);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据当前页码‘currentPageNum’和页的大小‘pageSize’从数据库中查出该页的记录
     * @Date: 2019/11/26 11:11
     * @Param: [currentPageNum, pageSize]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @ResponseBody
    @RequestMapping("/getUnitList")
    public DataPage unitList(int currentPageNum, int pageSize) {
        DataPage unitPage = unitService.selectPageUnit(currentPageNum, pageSize);
        return unitPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 接收‘单位注册’提交的数据并返回写入数据库的结果（成功|失败）
     * @Date: 2019/11/26 11:14
     * @Param: [unit, province, city, county]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/receiveUnitRegiste")
    public ResponseResult receiveUnitRegiste(Unit unit, long province, long city, long county) {
        //设置医院ID，从100000开始，递增。
        long maxUnitId = unitService.selectMaxUnitId();
        if (maxUnitId == 0) {
            unit.setUnitId(100000);
        } else {
            unit.setUnitId(maxUnitId + 1);
        }

        //设置注册日期和更新日期
        Date date = new Date(System.currentTimeMillis());
        unit.setRegisteTime(date);
        unit.setUpdateTime(date);

        //合并地址
        String provinceName = cityService.selectSpecificCityById(province).getCityName();
        String cityName = cityService.selectSpecificCityById(city).getCityName();
        String countyName = cityService.selectSpecificCityById(county).getCityName();
        unit.setAddress(provinceName + cityName + countyName + unit.getAddress());

        ResponseResult responseResult = new ResponseResult();
        //写入数据库
        ResponseResult result = unitService.registeUnit(unit);
        if (result.getCode()==0) {
            return result;
        }
        List<Unit> unitList=unitService.selectUnitByName(unit);
        //获取刚才注册的单位的id
        long unitId=unitList.get(0).getUnitId();
        //创建该单位的默认超级管理员角色
        ResponseResult createSuperAdminRoleResult=roleService.addRole(unitId, "superAdmin", "超级管理员", "4,5,6,7,8,9,10,11,12,13,14,15,16,17,");
        Role role=roleService.getRoleByName("superAdmin","超级管理员", unitId);
        ResponseResult apportionRoleResult=roleService.executeApportionRole(Long.valueOf(unitId+"000000"), role.getRoleId()+",");

        if(createSuperAdminRoleResult.getCode()==0){
            responseResult.setCode(0);
            responseResult.setMsg("注册失败！");
            return responseResult;
        }
        if(apportionRoleResult.getCode()==0){
            responseResult.setCode(0);
            responseResult.setMsg("注册失败！");
            return responseResult;
        }
        responseResult.setCode(1);
        responseResult.setMsg("注册成功！");
        return responseResult;

    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 更新’体检单位’的启用状态unit.disabled
     * @Date: 2019/11/26 11:35
     * @Param: [disabled, unitId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/updateUnitDisabled")
    public ResponseResult updateUnitDisabled(boolean disabled, long unitId) {
        ResponseResult responseResult = unitService.updateUnitDisabled(disabled, unitId);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据UnitId删除表unit中的一条记录
     * @Date: 2019/11/26 15:16
     * @Param: [unitId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/deleteUnitById")
    public ResponseResult deleteUnitById(long unitId) {
        int result = unitService.deleteUnitById(unitId);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(result);
        if (result == 1) {
            responseResult.setMsg("删除成功");
        } else {
            responseResult.setMsg("删除失败");
        }
        return responseResult;
    }


    @RequestMapping("/getEditUnitPage")
    public String getEditUnitPage() {
        return "unit/unitEdit.html";
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 批量删除单位
     * @Date: 2019/11/27 19:46
     * @Param: [unitIds]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/batchDeleteUnits")
    public ResponseResult batchDeleteUnit(String unitIds) {
        //去掉最后面的一个逗号
        unitIds = unitIds.substring(0, unitIds.length() - 1);
        int result = unitService.deleteBatchUnitsByIds(unitIds);
        ResponseResult responseResult = new ResponseResult();
        if (result == unitIds.split(",").length) {
            responseResult.setMsg("批量删除成功");
            responseResult.setCode(1);
        } else {
            responseResult.setMsg("批量删除失败");
            responseResult.setCode(0);
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 批量启用单位
     * @Date: 2019/11/27 21:04
     * @Param: [unitIds]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/batchEnabledUnits")
    public ResponseResult batchEnabledUnits(String unitIds) {
        unitIds = unitIds.substring(0, unitIds.length() - 1);
        int result = unitService.updateBatchEnabledUnitsByIds(unitIds);
        ResponseResult responseResult = new ResponseResult();
        if (result == unitIds.split(",").length) {
            responseResult.setCode(1);
            responseResult.setMsg("批量启用成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("批量启用失败");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 批量停用单位
     * @Date: 2019/11/27 21:03
     * @Param: [unitIds]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/batchDisabledUnits")
    public ResponseResult batchDisabledUnits(String unitIds) {
        unitIds = unitIds.substring(0, unitIds.length() - 1);
        int result = unitService.updateBatchDisabledUnitsByIds(unitIds);
        ResponseResult responseResult = new ResponseResult();
        if (result == unitIds.split(",").length) {
            responseResult.setCode(1);
            responseResult.setMsg("批量停用成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("批量停用失败");
        }
        return responseResult;
    }

}
